﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;

namespace HMEngine.HMEffects {
    public class HMEffectManager : DrawableGameComponent {
        private static readonly Dictionary<string, HMShader> myShaders = new Dictionary<string, HMShader>();
        private static readonly Dictionary<string, HMPostProcessor> myPostProcessors = new Dictionary<string, HMPostProcessor>();

        public static HMShader ActiveShader { get; private set; }

        public HMEffectManager(Game game) : base(game) { }

        public static void AddShader(string shaderLabel, HMShader newShader) {
            myShaders.Add(shaderLabel, newShader);
        }

        public static void AddPostProcessor(string ppLabel, HMPostProcessor newPostProcessor) {
            myPostProcessors.Add(ppLabel, newPostProcessor);
        }

        internal static void SetActiveShader(string effectLabel) {
            if (null != effectLabel && myShaders.ContainsKey(effectLabel)) {
                ActiveShader = myShaders[effectLabel];
            } else {
                ActiveShader = myShaders["HMEngine.HMEffects.BasicShader"];
            }
        }

        public override void Initialize() {
            HMShader BasicShader = new HMShader("HMContent/Shaders/BasicShader");
            myShaders.Add("HMEngine.HMEffects.BasicShader", BasicShader);

            base.Initialize();
        }

        protected override void LoadContent() {
            foreach (HMShader shader in myShaders.Values) {
                if(shader != null) shader.LoadContent(GraphicsDevice, Game.Content);
            }

            foreach (HMPostProcessor postProcessor in myPostProcessors.Values) {
                postProcessor.LoadContent(GraphicsDevice, Game.Content);
            }

            base.LoadContent();
        }

        public override void Draw(GameTime gameTime) {
            foreach (HMPostProcessor postProcessor in myPostProcessors.Values) {
                postProcessor.Draw(GraphicsDevice);
            }

            base.Draw(gameTime);
        }
    }
}